﻿Imports Aricie.DNN.Services
Imports Aricie.DNN.Settings
Imports Aricie.DNN.Modules.LuceneSearch.Settings
Imports Aricie.DNN.Diagnostics
Imports System.IO

Namespace Aricie.DNN.Modules.LuceneSearch.Logs
    Public Class FilterLogController

#Region "Shared Members & Constant"

        Private Const glbLuceneFilterLogFileNameBase As String = "Aricie.LuceneSearch.FilterLog."
        Private Const glbLuceneFilterLogFolderName As String = "Aricie.LuceneLog\"
        Private _moduleId As Integer
#End Region

        Public Sub New(ByVal moduleId As Integer)
            _moduleId = moduleId
        End Sub

#Region "Private Fields"

        Private _FilterLogFolderMapPath As String = ""
        Private _FilterLogFileMapPath As String = ""

#End Region

#Region "Properties"

        Private ReadOnly Property FilterLogFolderMapPath() As String
            Get
                If _FilterLogFolderMapPath = "" Then
                    _FilterLogFolderMapPath = NukeHelper.PortalSettings.HomeDirectoryMapPath & glbLuceneFilterLogFolderName
                End If
                Return _FilterLogFolderMapPath
            End Get
        End Property

        Private ReadOnly Property FilterLogFileMapPath() As String
            Get
                If _FilterLogFileMapPath = "" Then
                    _FilterLogFileMapPath = Me.FilterLogFolderMapPath & glbLuceneFilterLogFileNameBase & _moduleId & ".xml"
                End If
                Return _FilterLogFileMapPath
            End Get
        End Property

        Private ReadOnly Property LucenePortalSettings() As LucenePortalSettings
            Get
                Return GetModuleSettings(Of LucenePortalSettings)(SettingsScope.PortalSettings, NukeHelper.PortalId)
            End Get
        End Property

        Public ReadOnly Property FilterLogList() As List(Of FilterLogInfo)
            Get
                Return ExternalFileAsyncLogger(Of FilterLogInfo).GetCurrentLogList(Me.FilterLogFileMapPath)
            End Get
        End Property

#End Region

#Region "Public Sub"

        Public Function ReadFilterLog(ByVal StartDate As DateTime, ByVal EndDate As DateTime) As List(Of FilterLogInfo)
            Dim ToReturn As New List(Of FilterLogInfo)
            For Each item As FilterLogInfo In FilterLogList
                If ((DateTime.Compare(StartDate, item.LogDate) <= 0) And (DateTime.Compare(item.LogDate, EndDate) <= 0)) Then
                    ToReturn.Add(item)
                End If
            Next
            Return ToReturn
        End Function

        Public Sub UpdateFilterLog(ByVal logFilter As FilterLogInfo)

            Dim asyncLog As New ExternalFileLog(Of FilterLogInfo)(Me.FilterLogFileMapPath, logFilter)
            If LucenePortalSettings.PurgeLogNbDays <> 0 Then
                asyncLog.PurgeFilter = New Predicate(Of FilterLogInfo)(AddressOf New FilterLogPurger(TimeSpan.FromDays(LucenePortalSettings.PurgeLogNbDays)).PurgePredicate)
            End If
            ExternalFileAsyncLogger(Of FilterLogInfo).Instance.AddLog(asyncLog)

        End Sub

        Public Sub ClearFilterLog()
            'Suppression du fichier de log
            If File.Exists(FilterLogFileMapPath) Then
                File.Delete(FilterLogFileMapPath)
            End If
        End Sub

#End Region

    End Class

End Namespace